package com.ruoyi.project.kbjz.controller;

import com.alipay.api.domain.TableInfoResult;
import com.alipay.api.domain.UserDetails;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.kbjz.domain.KbjzCase;
import com.ruoyi.project.kbjz.service.IKbjzCaseService;
import com.ruoyi.project.kbjz.service.IKbjzRegisterService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/kbjz/case/")
public class KbjzCaseController extends BaseController {
    @Autowired
    private IKbjzCaseService kbjzCaseService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IKbjzRegisterService kbjzRegisterService;

    /**
     * 添加病例
     * @param kbjzCase
     * @return
     */
    @PostMapping("/addCase")
    public AjaxResult addCase(KbjzCase kbjzCase){
        String username = SecurityUtils.getUsername();
        //获取登录的用户
        kbjzCase.setCreateBy(username);
        kbjzCase.setDoctorName(username);
        //获取该用户的所有信息
        SysUser sysUser = sysUserService.selectUserByUserName(username);
        kbjzCase.setUserId(sysUser.getUserId());
        kbjzCase.setDeptId(sysUser.getDeptId());
        kbjzCase.setDeptName(sysUser.getDept().getDeptName());
        String result = kbjzCaseService.insertCase(kbjzCase);
        if (result != null){
            kbjzRegisterService.updateRegisterUserId(kbjzCase.getUserId(),kbjzCase.getRegisterId());
            return AjaxResult.success("病例添加成功",result);
        }else{
            return AjaxResult.error("病例添加失败");
        }
    }

    /**
     * 查询患者的历史病例
     * @param patientId 患者的id
     * @return
     */
    @GetMapping("/selectCaseByPatientId/{patientId}")
    public TableDataInfo selectCaseByPatientId(@PathVariable String patientId ){
        startPage();
        List<KbjzCase> kbjzCases = kbjzCaseService.selectCaseByPatientId(patientId);
        return getDataTable(kbjzCases);
    }

}
